package com.renting.modules.sys.service;

import com.renting.common.utils.excel.ExcelUtils;
import com.renting.modules.sys.entity.User;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2018/4/10.
 */
@Service
public class UserUploadService {

    @Autowired
    UserService userService;

    /**
     * excel读取入库
     */
    public void uploadExcel(MultipartFile[] files) {
        for (int i = 0; i < files.length; i++) {
            if (i == 0) {
                resolve(files[0]);//不管你上传多少 我只处理一个
                break;
            }
        }
    }

    /**
     * 处理文件
     *
     * @param file
     */
    private void resolve(MultipartFile file) {
        Workbook book = null;
        try {
            book = ExcelUtils.getWorkbook(file);
            Sheet sheet = book.getSheetAt(0);//默认处理第一个工作表
            List<User> list = drawUsers(sheet);
            userService.saveUserList(list);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 提取所有用户
     *
     * @param sheet
     * @return
     */
    private List<User> drawUsers(Sheet sheet) {
        List<User> users = new ArrayList<>();
        for (int numRow = 1; numRow <= sheet.getPhysicalNumberOfRows(); numRow++) {
            Row row = sheet.getRow(numRow);
            if (row == null) {
                continue;
            }
            int cells = row.getPhysicalNumberOfCells();
            if (cells < 3) {
                continue;
            }
            users.add(drawUser(row));
        }
        return users;
    }

    /**
     * 提取单个用户
     *
     * @param row
     * @return
     */
    private User drawUser(Row row) {
        User user = new User();
        Cell nameCell = row.getCell(0);
        user.setName(StringUtils.stripToEmpty(ExcelUtils.getValue(nameCell)).trim());
        Cell genderCell = row.getCell(1);
        user.setGender(StringUtils.stripToEmpty(ExcelUtils.getValue(genderCell)).trim());
        Cell nickNameCell = row.getCell(2);
        user.setNickName(StringUtils.stripToEmpty(ExcelUtils.getValue(nickNameCell)).trim());
        Cell phoneCell = row.getCell(3);
        user.setPhone(StringUtils.stripToEmpty(ExcelUtils.getValue(phoneCell)).trim());
        Cell idCardCell = row.getCell(4);
        user.setIdentityCard(StringUtils.stripToEmpty(ExcelUtils.getValue(idCardCell)).trim());
        Cell zmScoreCell = row.getCell(5);
        user.setZmScore(StringUtils.stripToEmpty(ExcelUtils.getValue(zmScoreCell)).trim());
        user.setId(UUID.randomUUID().toString());
        return user;

    }
}
